# 使用信号量实现线程同步
import logging
import threading
import time
import random

LOG_FORMAT = '%(asctime)s %(threadName)-17s %(levelname)-8s %(message)s'
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
semaphore = threading.Semaphore(0)
item = 0


def consumer():
    logging.info('Consumer is waiting')
    semaphore.acquire()  # 每次询问都是0，访问信号量变为-1，此时不能获得资源，该线程要挂掉，等待线程2释放资源，不能实现多次等待
    logging.info('Consumer notify:item number {}'.format(item))


def producer():
    global item
    time.sleep(3)
    item = random.randint(0, 1000)
    logging.info('Producer notify:item number {}'.format(item))
    semaphore.release()  # 生产完成以后释放信号量，线程1得以继续进行


def main():
    for i in range(10):
        t1 = threading.Thread(target=consumer)
        t2 = threading.Thread(target=producer)

        t1.start()
        t2.start()

        t1.join()
        t2.join()


if __name__ == '__main__':
    main()
